home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 20
/
Cream of the Crop 20 (Terry Blount) (1996).iso
/
program
/
vol15n11.zip
/
JAVAP.ZIP
/
JAVAPP.ZIP
/
SIMPLE.JAV
< prev
next >
Wrap
Text File
|
1996-03-26
|
3KB
|
122 lines
import java.io.*;
import java.awt.image.*;
import java.awt.*;
import java.net.*;
public class Simple extends java.applet.Applet implements Runnable, ImageObserver {
Image img;
int xpos = 0, direction, imgWidth, imgHeight, stepSize, interval;
Thread moveImage = null;
// Applet functions
public void init() {
img = getImage(getCodeBase(), "pcmag.gif");
// Typecast parameters to 0
String ap = getParameter("STEPSIZE");
stepSize = (ap != null) ? Integer.valueOf(ap).intValue() : 5;
ap = getParameter("INTERVAL");
interval = (ap != null) ? Integer.valueOf(ap).intValue() : 100;
img.getWidth(this);
//{{INIT_CONTROLS
setLayout(null);
addNotify();
resize(insets().left + insets().right + 474, insets().top + insets().bottom + 225);
btnStart=new Button("&Start");
add(btnStart);
btnStart.reshape(insets().left + 7,insets().top + 165,77,30);
btnStop=new Button("S&top");
add(btnStop);
btnStop.reshape(insets().left + 91,insets().top + 165,77,30);
//}}
}
public void paint(Graphics g) {
Dimension d = size();
g.setColor(Color.gray);
g.drawRect(0, 0, d.width - 1, d.height - 1);
g.drawImage(img, xpos, 50, null);
}
public void start() {
if (moveImage == null) {
moveImage = new Thread(this);
moveImage.start();
direction = stepSize;
}
}
public void stop() {
moveImage = null;
}
// Runnable Interface functions
public void run() {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true) {
try {
repaint();
Thread.sleep(interval);
xpos += direction;
if (xpos > size().width - imgWidth) {
direction = -stepSize;
xpos = size().width - imgWidth;
}
else if (xpos < 0) {
direction = stepSize;
xpos = 0;
}
} catch (InterruptedException e) {
moveImage.destroy();
return;
}
}
}
// ImageObserver Interface functions
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
imgWidth = width;
imgHeight = height;
return false;
}
//{{DECLARE_CONTROLS
Button btnStart;
Button btnStop;
//}}
public void clickedBtnStart() {
moveImage.resume();
}
public boolean handleEvent(Event event) {
if (event.id == Event.ACTION_EVENT && event.target == btnStop) {
clickedBtnStop();
return true;
}
else
if (event.id == Event.ACTION_EVENT && event.target == btnStart) {
clickedBtnStart();
return true;
}
return super.handleEvent(event);
}
public void clickedBtnStop() {
moveImage.suspend();
}
}